---
title: Image
description: Learn how to handle image inputs in BAML functions
---

Image values to BAML functions can be created in client libraries. This document explains how to use these functions both at compile time and runtime to handle image data. For more details, refer to [image types](/ref/baml/types#image).

## Usage Examples

<CodeBlocks>
```python
from baml_py import Image
from baml_client import b

async def test_image_input():
    # Create an Image from a URL
    img = Image.from_url("https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png")
    res = await b.TestImageInput(img=img)

    # Create an Image from Base64 data
    image_b64 = "iVB0xyz..."
    img = Image.from_base64("image/png", image_b64)
    res = await b.TestImageInput(img=img)
```

```typescript
import { b } from '../baml_client'
import { Image } from "@boundaryml/baml"

// Create an Image from a URL
let res = await b.TestImageInput(
    Image.fromUrl('https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png')
)

// Create an Image from Base64 data
const image_b64 = "iVB0xyz..."
res = await b.TestImageInput(
    Image.fromBase64('image/png', image_b64)
)

// Browser-specific methods
const fileImage = await Image.fromFile(file)
const blobImage = await Image.fromBlob(blob, 'image/png')
```

```tsx
import { useTestImageInput } from '../baml_client/react/hooks'
import { Image } from "../baml_client/react/media"

export function TestImageInput() {
    const { mutate } = useTestImageInput()

    const handleClick = async () => {
        const image = await Image.fromUrl('https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png')
        mutate(image)
    }

    return (
      <div>
          <button onClick={handleClick}>
              Test Image Input
          </button>
      </div>
    )
}
```

```go
package main

import (
    "context"
    
    b "example.com/myproject/baml_client"
)

func testImageInput() error {
    ctx := context.Background()
    
    // Create an Image from a URL
    img, err := b.NewImageFromUrl("https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png", nil)
    if err != nil {
        return err
    }
    
    result, err := b.TestImageInput(ctx, img)
    if err != nil {
        return err
    }

    // Create an Image from Base64 data
    imageB64 := "iVB0xyz..."
    img2, err := b.NewImageFromBase64(imageB64, stringPtr("image/png"))
    if err != nil {
        return err
    }
    
    result2, err := b.TestImageInput(ctx, img2)
    if err != nil {
        return err
    }
    
    return nil
}

// Helper function for string pointer
func stringPtr(s string) *string {
    return &s
}
```

```ruby
# Ruby implementation is in development.
```
</CodeBlocks>

## API Reference

<Tabs>
<Tab title="Python" language="python">

### Static Methods

<ParamField
  path="from_url"
  type="(url: str, media_type: Optional[str] = None) -> Image"
>
  Creates an Image object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.
</ParamField>

<ParamField
  path="from_base64"
  type="(media_type: str, base64: str) -> Image"
>
  Creates an Image object using Base64 encoded data along with the given MIME type.
</ParamField>

### Instance Methods

<ParamField
  path="is_url"
  type="() -> bool"
>
  Check if the image is stored as a URL.
</ParamField>

<ParamField
  path="as_url"
  type="() -> str"
>
  Get the URL of the image if it's stored as a URL. Raises an exception if the image is not stored as a URL.
</ParamField>

<ParamField
  path="as_base64"
  type="() -> list[str]"
>
  Get the base64 data and media type if the image is stored as base64. Returns `[base64_data, media_type]`. Raises an exception if the image is not stored as base64.
</ParamField>

<ParamField
  path="baml_serialize"
  type="() -> dict"
>
  Convert the image to a dictionary representation. Returns either `{"url": str}` or `{"base64": str, "media_type": str}`.
</ParamField>

</Tab>
<Tab title="TypeScript" language="typescript">

### Static Methods

<ParamField
  path="fromUrl"
  type="(url: string, mediaType?: string) => Image"
>
  Creates an Image object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.
</ParamField>

<ParamField
  path="fromBase64"
  type="(mediaType: string, base64: string) => Image"
>
  Creates an Image object using Base64 encoded data along with the given MIME type.
</ParamField>

<ParamField
  path="fromFile"
  type="(file: File) => Promise<Image>"
>
  <Info>Only available in browser environments. @boundaryml/baml/browser</Info>
  Creates an Image object from a File object. Available in browser environments only.
</ParamField>

<ParamField
  path="fromBlob"
  type="(blob: Blob, mediaType?: string) => Promise<Image>"
>
  <Info>Only available in browser environments. @boundaryml/baml/browser</Info>
  Creates an Image object from a Blob object. Available in browser environments only.
</ParamField>

<ParamField
  path="fromUrlToBase64"
  type="(url: string) => Promise<Image>"
>
  <Info>Only available in browser environments. </Info>
  Creates an Image object by fetching from a URL. Available in browser environments only.
</ParamField>

### Instance Methods

<ParamField
  path="isUrl"
  type="() => boolean"
>
  Check if the image is stored as a URL.
</ParamField>

<ParamField
  path="asUrl"
  type="() => string"
>
  Get the URL of the image if it's stored as a URL. Throws an Error if the image is not stored as a URL.
</ParamField>

<ParamField
  path="asBase64"
  type="() => [string, string]"
>
  Get the base64 data and media type if the image is stored as base64. Returns `[base64Data, mediaType]`. Throws an Error if the image is not stored as base64.
</ParamField>

<ParamField
  path="toJSON"
  type="() => { url: string } | { base64: string; media_type: string }"
>
  Convert the image to a JSON representation. Returns either a URL object or a base64 object with media type.
</ParamField>

</Tab>
<Tab title="Go" language="go">

### Static Methods

<ParamField
  path="NewImageFromUrl"
  type="(url string, mediaType *string) (*Image, error)"
>
  Creates an Image object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.
</ParamField>

<ParamField
  path="NewImageFromBase64"
  type="(base64 string, mediaType *string) (*Image, error)"
>
  Creates an Image object using Base64 encoded data along with the given MIME type.
</ParamField>

### Instance Methods

<ParamField
  path="IsUrl"
  type="() bool"
>
  Check if the image is stored as a URL.
</ParamField>

<ParamField
  path="AsUrl"
  type="() (string, error)"
>
  Get the URL of the image if it's stored as a URL. Returns an error if the image is not stored as a URL.
</ParamField>

<ParamField
  path="AsBase64"
  type="() (string, string, error)"
>
  Get the base64 data and media type if the image is stored as base64. Returns `(base64Data, mediaType, error)`. Returns an error if the image is not stored as base64.
</ParamField>

<ParamField
  path="ToJSON"
  type="() (map[string]interface{}, error)"
>
  Convert the image to a map representation. Returns either `{"url": string}` or `{"base64": string, "media_type": string}`.
</ParamField>

</Tab>
<Tab title="Ruby" language="ruby">

Ruby implementation is in development.

</Tab>
</Tabs>

## URL Handling

When you create an Image using `from_url`, BAML processes the URL according to your client's `media_url_handler` configuration:

- **[OpenAI](/ref/llm-client-providers/open-ai#media_url_handler)**: By default keeps URLs as-is (`send_url`). Set to `send_base64` to convert to base64.
- **[Anthropic](/ref/llm-client-providers/anthropic#media_url_handler)**: By default keeps URLs as-is (`send_url`). The provider accepts both formats.
- **[Google AI](/ref/llm-client-providers/google-ai-gemini#media_url_handler)**: By default uses `send_base64_unless_google_url` to preserve gs:// URLs while converting others.
- **[Vertex AI](/ref/llm-client-providers/google-vertex#media_url_handler)**: By default uses `send_url_add_mime_type` to include MIME type information.
- **[AWS Bedrock](/ref/llm-client-providers/aws-bedrock#media_url_handler)**: By default converts to base64 (`send_base64`).

You can override these defaults in your client configuration. See the provider-specific documentation linked above for details.